package org.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.entity.DepartmentalStructure;
import org.example.entity.PersonalTaxRules;
import org.example.entity.SecurityProvident;
import org.example.models.R;
import org.example.service.PersonalTaxRulesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/salary/setup/rate")
public class PersonalTaxRulesController {
    @Autowired
    PersonalTaxRulesService personalTaxRulesService;
    @GetMapping()
    public R<Page<PersonalTaxRules>> page(
            @RequestParam(name="current",defaultValue = "1") int current,
            @RequestParam(name="size",defaultValue = "10") int size,
            @RequestParam(name="a",required = false) String aStr,
            @RequestParam(name="createdBy",required = false) String createdBy
    ){
        QueryWrapper<PersonalTaxRules> queryWrapper = new QueryWrapper<>();
        if(aStr != null && !aStr.isEmpty()){
            try {
                Integer a = Integer.parseInt(aStr);  // 转换为整数
                queryWrapper.lambda()
                        .le(PersonalTaxRules::getLowerLimit, a)  // 用数值类型比较
                        .ge(PersonalTaxRules::getUpperLimit, a);
            } catch (NumberFormatException e) {
                // 处理转换失败的情况（如输入非数字）
                return R.error("请输入有效的数字");
            }
        }
        if(createdBy != null){
            queryWrapper.lambda().like(PersonalTaxRules::getCreatedBy,createdBy);

        }

        Page<PersonalTaxRules> page = new Page<>(current, size);

        personalTaxRulesService.page(page,queryWrapper);
        return R.success(page);
    }
    @GetMapping("/app")
    public R<Page<PersonalTaxRules>> pages(
            @RequestParam(name="current",defaultValue = "1") int current,
            @RequestParam(name="size",defaultValue = "10") int size,
            @RequestParam(name="a",required = false) String aStr,
            @RequestParam(name="createdBy",required = false) String createdBy
    ){
        QueryWrapper<PersonalTaxRules> queryWrapper = new QueryWrapper<>();
        if(aStr != null && !aStr.isEmpty()){
            try {
                Integer a = Integer.parseInt(aStr);  // 转换为整数
                queryWrapper.lambda()
                        .le(PersonalTaxRules::getLowerLimit, a)  // 用数值类型比较
                        .ge(PersonalTaxRules::getUpperLimit, a);
            } catch (NumberFormatException e) {
                // 处理转换失败的情况（如输入非数字）
                return R.error("请输入有效的数字");
            }
        }
        if(createdBy != null){
            queryWrapper.lambda().like(PersonalTaxRules::getCreatedBy,createdBy);

        }
            queryWrapper.lambda().eq(PersonalTaxRules::getEnable,"启用");
        Page<PersonalTaxRules> page = new Page<>(current, size);

        personalTaxRulesService.page(page,queryWrapper);
        return R.success(page);
    }
    @PostMapping("/insert")
    public R insert(@RequestBody PersonalTaxRules personalTaxRules,@RequestAttribute("username") String username){

        personalTaxRules.setDate(new Date());
        personalTaxRules.setUpDate(new Date());
        personalTaxRules.setCreatedBy(username);
        personalTaxRulesService.insert(personalTaxRules);
        return R.success();
    }
    @PutMapping("/update")
    public R update(@RequestBody PersonalTaxRules personalTaxRules,@RequestAttribute("username") String username){
        personalTaxRules.setUpDate(new Date());
        personalTaxRules.setCreatedBy(username);
        personalTaxRulesService.updateByPrimaryKeySelective(personalTaxRules);
        return R.success();
    }

}
